﻿// © Benn Eichhorn 2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.Feeds.Finance.Chix.CHIXMD.Messages
{
    /// <summary>
    /// Singleton Chi-X System Event message
    /// </summary>
    public class SystemEvent : IMessage
    {
        private SystemEvent() { }

        private static SystemEvent m_Instance = new SystemEvent();

        /// <summary>
        /// Get's the singleton instance of the SystemEvent class
        /// </summary>
        /// <returns>Current SystemEvent</returns>
        public static SystemEvent GetInstance() { return m_Instance; }
         

        public char MessageType { get { return 'S'; } }
        /// <summary>
        /// Milliseconds since midnight
        /// </summary>
        public int TimeStampMillis;

        /// <summary>
        /// Slow, calculates current TimeStamp from TimeStampMillis
        /// </summary>
        public DateTime TimeStamp { get { return MessageParser.Date.AddMilliseconds(TimeStampMillis); } }

        public char EventCodeChar;

        /// <summary>
        /// Chi-X System Event
        /// </summary>
        public EventCodes EventCode;

        public enum EventCodes
        {
            UnhandledSystemEvent,
            /// <summary>
            /// Event Code S - Start Of Day
            /// </summary>
            StartOfDay,
            /// <summary>
            /// Event Code E - End Of Day
            /// </summary>
            EndOfDay
        }

        public override string ToString()
        {
            return "IMessage->SystemEvent:EventCode=" + EventCode.ToString();
        }


        const int S_S_EVENTCODE_LOC = 10;


        internal static IMessage Parse(ref string inbound)
        {
            // get sequenced data message timestamp
            if (!Int32.TryParse(inbound.Substring(MessageParser.S_TIMESTAMP_LOC, MessageParser.S_TIMESTAMP_LEN), out m_Instance.TimeStampMillis))
            {
                Messages.Error message = Messages.Error.GetInstance("Sequenced data message failed to parse TimeStamp millis as Double", inbound);
                return message;
            }

            m_Instance.EventCodeChar = inbound[S_S_EVENTCODE_LOC];

            switch (m_Instance.EventCodeChar)
            {
                case 'S':
                    m_Instance.EventCode = EventCodes.StartOfDay;
                    break;
                case 'E':
                    m_Instance.EventCode = EventCodes.EndOfDay;
                    break;
                default:
                    m_Instance.EventCode = EventCodes.UnhandledSystemEvent;
                    break;
            }

            return m_Instance;
        }


        /// <summary>
        /// Deep clone
        /// </summary>
        /// <returns></returns>
        public SystemEvent Clone()
        {
            SystemEvent clone = new SystemEvent();
            clone.EventCode = m_Instance.EventCode;
            clone.EventCodeChar = m_Instance.EventCodeChar;
            clone.TimeStampMillis = m_Instance.TimeStampMillis;
            return clone;
        }


    }
}
